<?php

class Parser {

    public static function getHtmlPage($url, $proxy = false) {
        
        if (!$proxy) {
            //$proxy = Yii::app()->params['proxy'];
            $proxy = array();
            $criteria = new CDbCriteria;
            $criteria->condition = 'is_active = 1';
            $models = Proxy::model()->findAll($criteria);
            foreach($models as $model){
                $proxy[] = $model->ip;
            }
            
            if(empty($proxy)) { 
                $proxy = Yii::app()->params['proxy'];
            }
            
            $rand_keys = array_rand($proxy);
            $proxy = $proxy[$rand_keys];
        }
        
        $userAgent = Yii::app()->params['userAgent'];
        $rand_keys = array_rand($userAgent);
        $userAgent = $userAgent[$rand_keys];
         
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        curl_setopt($ch, CURLOPT_PROXY, $proxy);

        $html = curl_exec($ch);
                
        curl_close($ch);
        
        return $html;
    }

}




